/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.gui;

import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import model.dao.DaoAluno;
import model.dominio.Aluno;
import model.dominio.Turma;

/**
 *
 * @author Teao
 */
public class GuiAlunoModificar extends javax.swing.JPanel {

    private List<Aluno> listaAluno;
    private List<Turma> listaTurma;
    private Aluno aluno = new Aluno();

    public GuiAlunoModificar() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        SelBusca = new javax.swing.ButtonGroup();
        jDialog1 = new javax.swing.JDialog();
        buscarAluno = new javax.swing.JButton();
        salvar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabAluno = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        nome = new javax.swing.JTextField();
        comboAlunos = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        removerAluno = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        cpf = new javax.swing.JFormattedTextField();

        setLayout(null);

        buscarAluno.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/gui/icones/buscar.png"))); // NOI18N
        buscarAluno.setText("BUSCAR");
        buscarAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarAlunoActionPerformed(evt);
            }
        });
        add(buscarAluno);
        buscarAluno.setBounds(520, 10, 130, 50);

        salvar.setText("Salvar");
        salvar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarActionPerformed(evt);
            }
        });
        add(salvar);
        salvar.setBounds(480, 330, 170, 50);

        tabAluno.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        tabAluno.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "", "Ano", "Periodo", "Disciplina", "Nota", "Falta", "Resultado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tabAluno.setToolTipText("");
        tabAluno.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_NEXT_COLUMN);
        jScrollPane2.setViewportView(tabAluno);

        add(jScrollPane2);
        jScrollPane2.setBounds(10, 110, 640, 200);

        jLabel2.setText("CPF:");
        add(jLabel2);
        jLabel2.setBounds(280, 20, 50, 30);

        nome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomeActionPerformed(evt);
            }
        });
        add(nome);
        nome.setBounds(50, 20, 210, 30);

        comboAlunos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboAlunosActionPerformed(evt);
            }
        });
        add(comboAlunos);
        comboAlunos.setBounds(60, 70, 590, 30);

        jLabel4.setText("Nome:");
        add(jLabel4);
        jLabel4.setBounds(10, 20, 50, 30);

        jLabel5.setText("Aluno:");
        add(jLabel5);
        jLabel5.setBounds(10, 70, 50, 30);

        removerAluno.setText("Remover Aluno");
        removerAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removerAlunoActionPerformed(evt);
            }
        });
        add(removerAluno);
        removerAluno.setBounds(50, 330, 120, 50);

        jButton1.setText("Remover dados");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(190, 330, 130, 50);

        try {
            cpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        add(cpf);
        cpf.setBounds(320, 20, 160, 30);
    }// </editor-fold>//GEN-END:initComponents

    private void buscarAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarAlunoActionPerformed
        // TODO add your handling code here:
        buscarAluno();
    }//GEN-LAST:event_buscarAlunoActionPerformed

    private void salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarActionPerformed
        // Ao clicar nesse botão, irá para uma caixa de dialogo
        // questionando o usuário se aceita excluir o dado em questão.
        // Se sim, gera uma outra caixa dizendo que o dado foi
        // excluido com sucesso, e exclui o(s) dado(s) também.

        // Se não, volta para o programa principal.

        // Ele irá remover o(s) aluno(s) selecionado(s) na tabela.

        Aluno aluno = new Aluno();
        if (nome.getText().isEmpty() || cpf.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Digite os dados corretamente", "ERRO", JOptionPane.ERROR_MESSAGE);
        } else {
            aluno.setNome(nome.getText());
            aluno.setCpf(cpf.getText());
            DaoAluno dao = new DaoAluno();
            aluno = (Aluno) dao.salvar(aluno);
            if (aluno != null) {
                JOptionPane.showMessageDialog(this, "Dados registrados com sucesso", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
                this.setVisible(false);
            }

        }
    }//GEN-LAST:event_salvarActionPerformed

    private void comboAlunosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboAlunosActionPerformed
        aluno = listaAluno.get(comboAlunos.getSelectedIndex());
        preencheTabelaTurmas(aluno);
    }//GEN-LAST:event_comboAlunosActionPerformed

    private void nomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nomeActionPerformed

    private void removerAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removerAlunoActionPerformed
        
            DaoAluno dao = new DaoAluno();
            if (dao.excluir(aluno)) {
                JOptionPane.showMessageDialog(this, "Dados excluidos com sucesso", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
                this.setVisible(false);
            }

    }//GEN-LAST:event_removerAlunoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup SelBusca;
    private javax.swing.JButton buscarAluno;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox comboAlunos;
    private javax.swing.JFormattedTextField cpf;
    private javax.swing.JButton jButton1;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField nome;
    private javax.swing.JButton removerAluno;
    private javax.swing.JButton salvar;
    private javax.swing.JTable tabAluno;
    // End of variables declaration//GEN-END:variables

//    public String[] listarAlunos() {
//        String [] nomes;
//        return({""});
//    }
    public void buscarAluno() {
        Aluno a = new Aluno();
        a.setNome(nome.getText());
        a.setCpf(cpf.getText());
        comboAlunos.removeAllItems();
        listaAluno = new DaoAluno().buscarAlunos(a);
        if (nome.getText().isEmpty() || cpf.getText().isEmpty() || listaAluno == null) {
            JOptionPane.showMessageDialog(this, "Os dados digitados estão incorretos ou aluno não existe", "ERRO", JOptionPane.ERROR_MESSAGE);
        }
        else {
            for (Aluno aluno : listaAluno) {
            comboAlunos.addItem(aluno.getNome() + " - " + aluno.getCpf());
            }
            if (listaAluno.size() == 1) {
                preencheTabelaTurmas(listaAluno.get(0));
            }
        }
    }

    public void preencheTabelaTurmas(Aluno aluno) {
        List<Turma> turmas = aluno.getTurmas();
        DefaultTableModel model = (DefaultTableModel) tabAluno.getModel();
        String[] linha =new String[7];
        for (Turma t : turmas) {
            linha[1] = Integer.toString(t.getAno());
            linha[2] = Integer.toString(t.getPeriodo());
            linha[3] = t.getDisciplina().getNome();
            linha[4] = t.getDisciplina().getNome();
            linha[5] = t.getDisciplina().getNome();
            linha[6] = t.getDisciplina().getNome();

            
            model.addRow(linha);
            
        }
    }
}